package com.ql.service.menzen;

import com.ql.dao.menzhen.FomularyMapper;
import com.ql.pojo.menzhen.Doctor;
import com.ql.pojo.menzhen.Fomulary;
import com.ql.pojo.menzhen.Guahaodan;
import com.ql.pojo.menzhen.MenzhengDoctor;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
@Service("fomularyService")
public class FomularyServiceImpl implements FomularyService {

    private static final Logger LOGGER = Logger.getLogger("FomularyServiceImpl.class");
    @Resource
    public FomularyMapper fomularyMapper;

    public List<Fomulary> getById(int uId) {
        List<Fomulary> fomularyList = new ArrayList<Fomulary>();

        try {
            fomularyList = fomularyMapper.qryAll(uId);
//            System.out.println(fomularyList.toString());
            if(fomularyList.size() <= 0){
                LOGGER.debug("FomularyServiceImpl.getById 失败");
            }else {
                LOGGER.debug("FomularyServiceImpl.getById 成功");
            }
        } catch (Exception e) {
            //e.printStackTrace();
            LOGGER.error("FomularyServiceImpl.getById 异常:"+e.getMessage());
        }

        return fomularyList;
    }

    @Override
    public Fomulary getFid(int fId) {
        Fomulary fomulary = null;

        try {
            fomulary = fomularyMapper.qryByFid(fId);
            if(fomulary == null){
                LOGGER.debug("FomularyServiceImpl.getFid 失败");
            }else {
                LOGGER.debug("FomularyServiceImpl.getFid 成功");
            }
        } catch (Exception e) {
            //e.printStackTrace();
            LOGGER.error("FomularyServiceImpl.getFid 异常:"+e.getMessage());
        }
        return fomulary;
    }

    @Override
    public boolean addFomulary(Fomulary fomulary) {
        boolean flag = false;

        try {
            int add = fomularyMapper.add(fomulary);
            System.out.println(add);
            if(add != 1){
                LOGGER.error("FomularyServiceImpl.addFomulary 失败");
            }else {
                LOGGER.error("FomularyServiceImpl.addFomulary 成功");
                flag = true;
            }
        } catch (Exception e) {
            //e.printStackTrace();
            LOGGER.error("FomularyServiceImpl.addFomulary 异常:"+e.getMessage());
        }

        return flag;
    }

    @Override
    public List<Doctor> getByIdDoctor(int kid) {
        List<Doctor> doctors = null;

        try {
            doctors = fomularyMapper.qryDoctor(kid);
//            System.out.println(doctors.toString());
            if(doctors.size() <= 0){
                LOGGER.debug("FomularyServiceImpl.getByIdDoctor 失败");
            }else {
                LOGGER.debug("FomularyServiceImpl.getByIdDoctor 成功");
            }
        } catch (Exception e) {
            //e.printStackTrace();
            LOGGER.error("FomularyServiceImpl.getByIdDoctor 异常:"+e.getMessage());
        }

        return doctors;
    }

    @Override
    public Doctor getDoctor(int dId) {
        Doctor doctor = null;
        try {
             doctor = fomularyMapper.qryByIdDoctor(dId);
            if(doctor == null){
                LOGGER.debug("FomularyServiceImpl.getDoctor 失败");
            }else {
                LOGGER.debug("FomularyServiceImpl.getDoctor 成功");
            }
        } catch (Exception e) {
            //e.printStackTrace();
            LOGGER.error("FomularyServiceImpl.getDoctor 异常:"+e.getMessage());
        }

        return doctor;
    }

    @Override
    public int savDoctor(MenzhengDoctor menzhengDoctor) {
        int row = 0;
        try {
             row = fomularyMapper.saveMenzheng(menzhengDoctor);
            if(row != 1){
                LOGGER.debug("FomularyServiceImpl.savDoctor 失败");
            }else {
                LOGGER.debug("FomularyServiceImpl.savDoctor 成功");
            }
        } catch (Exception e) {
//            e.printStackTrace();
            LOGGER.error("FomularyServiceImpl.savDoctor 异常"+e.getMessage());
        }

        return row;
    }

    @Override
    public int addGuahaodan(Guahaodan guahaodan) {
        int row =0;
        try {
            row = fomularyMapper.saveGuaHao(guahaodan);
            if(row != 1){
                LOGGER.debug("FomularyServiceImpl.addGuahaodan 失败");
            }else {
                LOGGER.debug("FomularyServiceImpl.addGuahaodan 成功");
            }
        } catch (Exception e) {
            //e.printStackTrace();
            LOGGER.error("FomularyServiceImpl.addGuahaodan 异常"+e.getMessage());
        }
        return row;
    }
}
